package com.holyant.goods.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.domain.BaseDto;
import com.ruoyi.common.core.validator.CreateGroup;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类dto
 * 
 * @author tswsoft
 * @date 2021-03-31
 */
@ApiModel("商品分类")
@Data
public class GdsCategoryDto extends BaseDto {

    private static final long serialVersionUID = 1L;

    /** 上级id */
    @JsonSerialize(using = ToStringSerializer.class)
    @JSONField(serializeUsing = com.alibaba.fastjson.serializer.ToStringSerializer.class)
    @NotNull(message = "父分类不能为空",groups = CreateGroup.class)
    private Long parentId;

    /** 分类编号 */
    private String code;

    /** 统一码 2位序列号 */
    private String uniformCode;

    /** 分类名称 */
    @Excel(name = "分类名称")
    @ApiModelProperty(value = "分类名称")
    private String name;

    /** 综合税率 */
    @Excel(name = "综合税率")
    @ApiModelProperty(value = "综合税率")
    private BigDecimal taxRate;

    /** 消费税率 */
    @Excel(name = "消费税率")
    @ApiModelProperty(value = "消费税率")
    private BigDecimal consumptionTaxRate;
    /** 综合税率 */
    @Excel(name = "增值税率")
    @ApiModelProperty(value = "增值税率")
    private BigDecimal valueAddedTaxRate;

    /** 商品属性 */
    @Excel(name = "商品属性")
    @ApiModelProperty(value = "商品属性")
    private String goodsAttr;

    /** sku属性 */
    @Excel(name = "sku属性")
    @ApiModelProperty(value = "sku属性")
    private String skuAttr;


    /** 备注 */
    @Excel(name = "备注")
    @ApiModelProperty(value = "备注")
    private String remark;

    /** 子分类 */
    @ApiModelProperty(value = "子分类")
    private List<GdsCategoryDto> children = new ArrayList<GdsCategoryDto>();

    // 效期预警
    private String expireWarningObj;

    public ExpireWarning getExpireWarning() {
        if (expireWarningObj == null) {
            return null;
        }
        return JSON.parseObject(expireWarningObj, ExpireWarning.class);
    }

    @Data
    @EqualsAndHashCode
    public static class ExpireWarning {
        private Integer expiry;
        private Integer clearance;

        public Boolean greaterThanZero() {
            return expiry != null && expiry > 0 && clearance != null && clearance > 0;
        }
    }
}
